﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using PPI.Entities.Inspections;
using VMind.Global;

namespace PPI.Client.Logic.Inspection
{
    public class LInspectionMap
    {

        public static List<EMap> SelectAll()
        {
            List<EMap> listMaps = new List<EMap>();
            DataTable dt = WebConsumer.Inspection.WInspectionMap.SelectAll().Tables[0];
            foreach (DataRow dr in dt.Rows)
            {
                Int32 parentPolygonId = ConvertHelper.ToInt32(dr["PolygonParentId"]);
                if(parentPolygonId == Int32.MinValue)
                {
                    EMap map = new EMap(ConvertHelper.ToInt32(dr["PolygonChildId"]),dr["PolygonChildName"].ToString());
                    listMaps.Add(map);
                }
            }

            foreach (DataRow dr in dt.Rows)
            {
                Int32 parentPolygonId = ConvertHelper.ToInt32(dr["PolygonParentId"]);
                if (parentPolygonId != Int32.MinValue)
                {
                    ESection section = new ESection(ConvertHelper.ToInt32(dr["PolygonChildId"]), dr["PolygonChildName"].ToString()); 
                    listMaps.Find(delegate(EMap m) { return m.Id == parentPolygonId; }).AddSection(section);
                }
            }

            return listMaps;
        }
    }
}
